<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>管理平台</title>
    <jsp:include page="../../include/header.jsp"></jsp:include>   
</head>
<body>
<div id="wrapper">

		<div class="row">
			<div class="col-lg-12">
				<h1 class="page-header">消息管理</h1>
			</div>
		</div>

		<div class="row">
			<div class="col-lg-12">
				<div class="row">
					<div class="col-lg-6">
						<div class="text-muted single-line-text pull-left">共 <font color="#428bca" id="dataCount">0</font> 条记录</div>
					</div>
					<div class="col-lg-6">
						<div class="pull-right">
						<button type="button" class="btn btn-success" onclick="toAdd()"><span class="fa fa-plus"></span>推送消息</button>
						</div>
					</div>
				</div>
				<div class="table-responsive">
					<table id="dataTable" class="table table-striped table-hover">
			            <thead>
			                <tr>
			                    <th>标题</th>
			                    <th>查看量</th> 
			                    <th>关闭</th>
			                    <th>领取</th>
			                    <th>操作</th>
			                </tr>
			            </thead>
			            <tbody id="dataList"></tbody>
			        </table>
				</div>
		        <div class="row">
		        	<div class="col-lg-12">
		        		<ul id="dataPagination" class="pagination-sm pull-right"></ul>
		        	</div>
		        </div>
			</div>
		</div>

</div>
<jsp:include page="../../include/javascripts.jsp"></jsp:include>
<!-- 异步加载下一页数据后，用模板渲染 -->
<script type="text/html" id="dataTpl">
	{{each list as item}}
	<tr id="item{{item.id}}" data-id="{{item.id}}">
		<td>{{item.title}}</td>
		<td>{{item.seeNum}}</td>
		<td>{{item.closeNum}}</td>
		<td>{{item.receive}}</td>
		<td>
			<button type="button" class="btn btn-sm btn-primary btn-simple" onclick="toDelete('{{item.id}}')" title="删除"><span class="fa fa-remove fa-lg"></span></button>
			<button type="button" class="btn btn-sm btn-primary btn-simple" onclick="toEdit('{{item.id}}')" title="编辑"><span class="fa fa-edit fa-lg"></span></button> 
			{{if 0 ==item.isClose}}
				<button type="button" class="btn btn-sm btn-danger btn-simple" onclick="updateProperty('{{item.id}}')" title="关闭活动" id="{{item.id}}a"><span class="fa fa-lg">关闭活动</span></button>
			{{else}}
				<button type="button" class="btn  btn-simple"  title="已"><span class="fa fa-lg" id="{{item.id}}b" >已经结束</span></button>
			{{/if}}
		</td>
	</tr>
	{{/each}}
</script>
<script type="text/javascript">
var dataPaginator;
  $(query);  

function query(){
	var params = {
			pageSize:12
		};
	if(dataPaginator){
		dataPaginator.destroy();
	}

	dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/wx/homeWindow/page",params,function(result){
		//设置显示最新的数据数量
		$("#dataCount").html(result.object.totalRow);
		//根据模板渲染数据并填充
		$("#dataList").empty().append(template("dataTpl",result.object));
	});
}
var addDialog;
function toAdd(){
	addDialog = Kit.dialog("添加","${BASE_PATH}/wx/homeWindow/toAdd?mp=${mp}",{size:"size-wide"}).open();
}
var editDialog;
function toEdit(id){
	editDialog = Kit.dialog("修改","${BASE_PATH}/wx/homeWindow/toEdit/"+id+"?mp=${mp}",{size:"size-wide"}).open();
}
function onEditSuccess(id,newObject){
	$("#item"+id).replaceWith(template("list-tpl",{"list":[newObject]}));
}
function updateProperty(id){
	var activ=document.querySelector("#dataPagination  .active a").text;
	var params = {
			pageNum:activ,
			pageSize:12
		};
	Kit.confirm("提示","您确定要结束活动吗？",function(){
		 $.post("${BASE_PATH}/wx/homeWindow/updateProperty/"+id,function(result){
			  dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/wx/homeWindow/page",params,function(result){
					//设置显示最新的数据数量
					$("#dataCount").html(result.object.totalRow);
					//根据模板渲染数据并填充
					$("#dataList").empty().append(template("dataTpl",result.object));
				});	 
				
		});  		
	});
}
function toDelete(id) {
	Kit.confirm("提示", "您确定要删除这条数据吗？", function() {
		$.post("${BASE_PATH}/wx/homeWindow/doDelete/" + id, function(
				result) {
			$("#item" + id).remove();
		});
	});
}

</script>
</body>
</html>